<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="form" :disabled="flag==1" v-loading="loading">
        <div class="myTitle">基本信息</div>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="BOM名称" prop="bomName" required>
              <div v-if="flag==1">{{ form.bomName }}</div>
              <el-input v-else v-model="form.bomName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="BOM编码" prop="bomCode" required>
              <div v-if="flag==1">{{ form.bomCode }}</div>
              <el-input v-else v-model="form.bomCode" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="productId" required>
              <div v-if="flag==1">{{ form.productName }}</div>
              <el-select v-else v-model="form.productId" filterable style="width: 100%"
                         @change="changeGood">
                <el-option
                  v-for="item in goodList"
                  :key="item.id"
                  :label="item.productName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格名称" prop="skuId" required>
              <div v-if="flag==1">{{ form.skuName }}</div>
              <el-select v-else v-model="form.skuId" filterable style="width: 100%">
                <el-option
                  v-for="item in skuList"
                  :key="item.id"
                  :label="item.skuName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="myTitle">物料信息</div>
        <div v-if="flag!=1">
          <el-button type="primary" plain @click="matList.push({})">新增</el-button>
          <el-button plain @click="matList=[]">清空</el-button>
        </div>
        <el-table
          :data="matList"
          max-height="300"
        >
          <el-table-column type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="materialId" label="物料名称" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.materialName }}</div>
              <el-select v-else v-model="scope.row.materialId" filterable style="width: 100%">
                <el-option
                  v-for="item in matSelect"
                  :key="item.id"
                  :label="item.productName"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="skuName" label="物料规格" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.skuName }}</div>
              <el-select v-else v-model="scope.row.skuId" filterable style="width: 100%">
                <el-option
                  v-for="item in skuSelect"
                  :key="item.id"
                  :label="item.skuName"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="materialType" label="物料类型" align="center">
            <template slot-scope="scope">
              <dict-tag v-if="flag==1" :options="dict.type.sys_material_type" :value="scope.row.materialType"/>
              <el-select v-else v-model="scope.row.materialType" filterable style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_material_type"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="master" label="主材料" align="center" width="100">
            <template slot-scope="scope">
              <dict-tag v-if="flag==1" :options="dict.type.sys_yes_no" :value="scope.row.master"/>
              <el-select v-else v-model="scope.row.master" style="width: 100%">
                <el-option
                  v-for="item in dict.type.sys_yes_no"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量（个）" align="center" width="100">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.quantity }}</div>
              <el-input v-else type="number" v-model="scope.row.quantity" :min="0" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="salePrice" label="尺寸（cm*cm）" align="center">
            <template slot-scope="scope">
              <div v-if="flag==1">{{ scope.row.size }}</div>
              <el-input v-else v-model="scope.row.size" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" v-if="flag!=1" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="deleteMat(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {listGood, listGoodSku} from "../../../api/imsMng";
import {addProduceBom, updateProduceBom} from "../../../api/produceMng";

export default {
  dicts: ['sys_yes_no', 'sys_material_type'],
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {},
      goodList: [],
      skuList: [],
      matList: [],
      skuSelect: []
    }
  },
  watch: {
    'form.productId'(e) {
      if (e) {
        listGoodSku({productId: this.form.productId, pageSize: -1}).then(res => {
          this.skuList = res.rows
        })
      }
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.matList = []
      this.resetForm('form')
      this.form = form
      this.load()
      if (flag) {
        this.matList = this.form.itemList
      }
    },
    load() {
      if (this.flag != 1) {
        listGood({productType: 1, pageSize: -1}).then(res => {
          this.goodList = res.rows
        })
        listGood({productType: 3, pageSize: -1}).then(res => {
          this.matSelect = res.rows;
        })
        listGoodSku({productType: 3, pageSize: -1}).then(res => {
          this.skuSelect = res.rows;
        })
      }
    },
    changeGood() {
      this.$set(this.form, 'skuId', '')
    },
    deleteMat(row) {
      this.matList = this.matList.filter(e => e != row)
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          if (!this.matList.length) {
            this.$message.error('物料信息不能为空')
            return
          }
          this.form.itemList = this.matList
          this.loading = true
          if (!this.flag) {
            addProduceBom(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            updateProduceBom(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
